﻿using admin.Model;
using admin.Repository;
using System;
using System.Collections.Generic;

namespace admin.Bussniess
{
    public class ManagerService
    {







        /// <summary>
        /// 登录验证服务
        /// </summary>
        /// <param name="name">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>返回值为管理员个体</returns>
        public Administrator SignVerify(string name,string password)
        {
            var repo = new AdminRepository();
            var admin = repo.QueryAdmin(name);
            if (admin == null)
                return admin;
            else if (admin.Password == password)
                return admin;
            else
                return null;
        }

        //销量统计
        public string GetBookSaleCount()
        {
            var repo = new AdminRepository();
            var allbook = repo.GetAllBook();
            int count = 0;
            for(int i = 0; i < allbook.Count; i++)
            {
                count += int.Parse(allbook[i].Countsale);
            }
            return count.ToString();
            //return repo.GetBookSaleCount();
        }

        //查询管理员
        public Administrator QueryAdmin(string id)
        {
            var repo = new AdminRepository();
            return repo.QueryAdmin(id);
        }
        //更新管理员信息
        public Administrator UpdateAdmin(Administrator admin)
        {
            var repo = new AdminRepository();
            return repo.UpdateAdmin(admin);
        }
        //更新管理员信息（未用）
        public Administrator UpdateAdmin(string id,string name,string phone)
        {
            var admin = new Administrator();
            admin.Id = id;
            admin.Name = name;
            admin.Phone = phone;
            var repo = new AdminRepository();
            var oldadmin = repo.QueryAdmin(id);
            admin.Password = oldadmin.Password;
            return repo.UpdateAdmin(admin);
        }
        //得到所有的用户
        public List<Customers> GetALlCustomer()
        {
            var customer = new AdminRepository();
            return customer.GetAllCustomer();
        }

        //得到所有作者文稿列表
        public List<Fileview> GetManuscript()
        {
            var manuscript = new AdminRepository();
            return manuscript.GetManuscript();
        }

        //得到所有的作者信息
        public List<Author> GetAllAuthor()
        {
            var author = new AdminRepository();
            return author.GetAllAuthor();
        }

        //得到所有作者-出书申请数-已出版书数的集合list
        public List<authorBookFor> GetAllAuthorBookFors()
        {
            var allauthor = GetAllAuthor();
            var repo = new AdminRepository();
            var authorbookforlist = new List<authorBookFor>();
            for(int i = 0; i < allauthor.Count; i++)
            {
                var authorbookfor = new authorBookFor();
                authorbookfor.author = allauthor[i];
                authorbookfor.bookapply = repo.GetAuthorBookFor(allauthor[i].Name).Count.ToString();
                authorbookfor.booklist = repo.GetAuthorBookOuted(allauthor[i].Id).Count.ToString();
                authorbookforlist.Add(authorbookfor);
            }
            return authorbookforlist;
        }

        //得到所有的订单信息
        public List<Order> GetAllOrder()
        {
            var order = new AdminRepository();
            return order.GetAllOrder();
        }

        //得到用户订单数（）
        public List<UserOrder> GetAllCustomerOrder()
        {
            var customers = GetALlCustomer();
            var userorderlist = new List<UserOrder>();
            var repo = new AdminRepository();
            for (int i = 0; i < customers.Count; i++)
            {
                var userorder = new UserOrder();
                userorder.User = customers[i];
                //repo.QueryOrderByID(customers[i]);
                userorder.Order = repo.GetCustomerOrder(customers[i].Cid);
                userorderlist.Add(userorder);
            }
            return userorderlist;
        }
        //查看单个用户订单
        public List<Order> GetAllCustomerOrder(string cid)
        {
            var repo = new AdminRepository();
            return repo.GetCustomerOrder(cid);
        }

        //查询图书by id
        public Book QueryBook(string id)
        {
            var book = new AdminRepository();
            var bookShow = book.QueryBook(id);
            return bookShow;
        }

        //查询用户 by id
        public Customers QuerCustomer(string cid)
        {
            var user = new AdminRepository();
            return user.QueryCustomer(cid);
        }

        //删除图书信息
        public bool DelBook(string id)
        {
            var book = new AdminRepository();
            return book.DelBook(id);
        }

        //查询所有图书信息
        public List<Book> GetAllBook()
        {
            var repo = new AdminRepository();
            var bookList = repo.GetAllBook();
            return bookList;
        }
         //得到所有图书和其作者信息
        public List<AuthorOfBook> GetAllBookAndAuthor()
        {
            var repo = new AdminRepository();
            var bookList = repo.GetAllBook();
            var authorbookList = new List<AuthorOfBook>();
            for(int i = 0; i < bookList.Count; i++)
            {
                var authorbook = new AuthorOfBook();
                authorbook.book = bookList[i];
                authorbook.authors = repo.GetAllAuthorOfBook(bookList[i].Isbn);
                authorbookList.Add(authorbook);
            }
            return authorbookList;
        }

        //查询所有管理员（未用）
        public List<Administrator> GetAllAdmin()
        {
            var repo = new AdminRepository();
            var AdminList = repo.GetAllAdmin();
            return AdminList;
        }

        //更新用户信息
        public bool UpdateUser(Customers customers)
        {
            var repo = new AdminRepository();
            return repo.UpdateUser(customers);
        }

        //删除用户
        public bool RemoveUser(Customers user)
        {
            var repo = new AdminRepository();
            return repo.RemoveUser(user);
        }

        //增加图书信息
        public bool AddBook(Book book)
        {
            var repo = new AdminRepository();
            return  repo.AddBook(book);
        }

        //查询图书信息 by ISBN
        public Book QueryBookByISBN(string isbn)
        {
            var repo = new AdminRepository();
            return repo.QueryBookByISBN(isbn);
        }
        //查询图书 by name
        public Book QueryBookByName(string name)
        {
            var repo = new AdminRepository();
            return repo.QueryBookByName(name);
        }

        //更新图书信息
        public bool UpdateBook(Book book)
        {
            var repo = new AdminRepository();
            return repo.UpdateBook(book);
        }
        //查询作者信息 by id
        public Author QueryAuthorByID(string id)
        {
            var repo = new AdminRepository();
            return repo.QueryAuthorByID(id);
        }

        //更新作者信息
        public bool UpdateAuthor(Author oldAuthor)
        {
            var repo = new AdminRepository();
            return repo.UpdateAuthor(oldAuthor);
        }

        //删除作者
        public bool RemoveAuthor(Author author)
        {
            var repo = new AdminRepository();
            return repo.RemoveAuthor(author);
        }

        //查询订单信息by id
        public Order QueryOrderByID(int id)
        {
            var repo = new AdminRepository();
            return repo.QueryOrderByID(id);
        }

        //更新订单信息
        public bool UpdateOrder(Order oldOrder)
        {
            var repo = new AdminRepository();
            return repo.UpdateOrder(oldOrder);
        }

        //订单删除
        public bool RemoveOrder(Order order)
        {
            var repo = new AdminRepository();
            return repo.RemoveOrder(order);
        }

        //查询文稿信息
        public Fileview QueryArticleByID(String id)
        {
            var repo = new AdminRepository();
            return repo.QueryArticleByID(id);
        }
        //删除文稿
        public bool RemoveArticle(Fileview article)
        {
            var repo = new AdminRepository();
            return repo.RemoveArticle(article);
        }

        //更新文稿信息
        public bool UpdateArticle(Fileview article)
        {
            var repo = new AdminRepository();
            return repo.UpdateArticle(article);
        }

        public string OrderCheckB(string[] oid)
        {
            var orderListOut = new List<Order>();
            var orderListReturn = new List<Order>();
            var orderstatu = oid[0];
            for (int j = 1; j < oid.Length; j++)
            {
                var id = int.Parse(oid[j]);
                var order = QueryOrderByID(id);
                if (order != null)
                {
                    if (order.OrderStatus == "等待发货")
                        orderListOut.Add(order);
                    else if (order.OrderStatus == "退书中")
                        orderListReturn.Add(order);
                    else return "false";
                }
            }
            if (orderstatu == "已发货")
            {
                for (int i = 0; i < orderListOut.Count; i++)
                {
                    orderListOut[i].OrderStatus = orderstatu;
                    if (UpdateOrder(orderListOut[i]))
                        continue;
                    else return "false";
                }
                return "ok";

            }
            else if (orderstatu == "已退货")
            {
                for (int i = 0; i < orderListReturn.Count; i++)
                {
                    orderListReturn[i].OrderStatus = orderstatu;
                    if (UpdateOrder(orderListReturn[i]))
                        continue;
                    else return "false";
                }
                return "ok";
            }
            else return "false";
        }
        //查看作家的出书申请
        public List<Fileview> GetAuthorBookforByName(string name)
        {
            var repo = new AdminRepository();
            return repo.GetAuthorBookFor(name);
        }

        //得到作家已出版书列表
        public List<Book> GetAuthorBookOuted(string id)
        {
            var repo = new AdminRepository();
            return repo.GetOneAuthorBookOuted(id);
        }

        //更新图书的库存，销量信息在退书成功时
        public bool UpdateBookByName(Order order)
        {
            var book = QueryBookByName(order.Bookname);
            book.Countsale = (decimal.Parse(book.Countsale) - order.Quantity).ToString();
            book.Inventory = book.Inventory + order.Quantity;
            if (decimal.Parse(book.Countsale) < 0)
                return false;
            else if (UpdateBook(book))
                return true;
            else return false;
        }

        //通过图书ISBN查询一本书的信息和其作者信息
        public AuthorOfBook QueryBookAndAuthorByISBN(string isbn)
        {
            var repo = new AdminRepository();
            return  repo.QueryBookAuthorByISBN(isbn);
        }

        //删除图书作者
        public bool RemoveBookAuthor(string aid, string isbn)
        {
            var repo = new AdminRepository();
            return repo.RemoveAuthorBook(aid, isbn);
        }
        //增加图书作者
        public bool AddBookAuthor(string aid, string isbn)
        {
            var repo = new AdminRepository();
            return repo.AddAuthorBook(aid, isbn);
        }
    }
}
